#pragma once

#include "db.h"
#include <string>
#include <thread>
#include <atomic>
#include <vector>
#include <queue>
#include <mutex>
#include <condition_variable>

namespace FileScanner {

class Scanner {
public:
    Scanner(const std::string& directory,
           long long minSize,
           int workerCount,
           Database& db);

    /**
     * 开始扫描目录
     */
    void scan();

    /**
     * 检查数据库中的文件是否仍然存在
     */
    void checkIntegrity();

private:
    std::string directory_;
    long long minSize_;
    int workerCount_;
    Database& db_;

    // 统计信息
    std::atomic<long long> processedCount_{0};
    std::atomic<long long> totalCount_{0};
    std::atomic<long long> newFilesCount_{0};
    std::atomic<long long> skippedCount_{0};

    // 线程池相关
    std::queue<std::string> fileQueue_;
    std::mutex queueMutex_;
    std::condition_variable queueCondition_;
    std::atomic<bool> scanning_;
    std::atomic<bool> filesCollected_;

    /**
     * 收集所有需要处理的文件
     */
    void collectFiles();

    /**
     * 工作线程函数
     */
    void workerThread();

    /**
     * 处理单个文件
     */
    void processFile(const std::string& filePath);
};

} // namespace FileScanner
